package com.heima.admin.controller.v1;

import com.heima.admin.service.AdChannelService;
import com.heima.common.exception.CustException;
import com.heima.common.exception.CustomException;
import com.heima.model.admin.dtos.ChannelDTO;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.common.validate.ValidatorAdd;
import com.heima.model.common.validate.ValidatorUpdate;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @作者 itcast
 * @创建日期 2021/9/19 11:01
 **/
@Api(value = "频道管理API接口",tags = "频道管理API接口",description = "管理频道相关接口")
@RestController
@RequestMapping("/api/v1/channel")
public class ChannelController {
    @Autowired
    AdChannelService adChannelService;

    @ApiOperation("根据id查询频道")
    @GetMapping("/one/{id}")
    public ResponseResult findOne(@PathVariable Integer id) {
        return ResponseResult.okResult(adChannelService.getById(id));
    }

    @ApiOperation("查询全部频道")
    @GetMapping("/channels")
    public ResponseResult findAll() {
        List<AdChannel> list = adChannelService.list();
        return ResponseResult.okResult(list);
    }


    @ApiOperation(value = "分页查询频道列表",notes = "按照name模糊查询，分页查询 按照ord升序排序")
    @PostMapping("list")
    public ResponseResult list(@RequestBody ChannelDTO dto){
        return adChannelService.findByNameAndPage(dto);
    }

    @ApiOperation("频道新增")
    @PostMapping("/save")
    public ResponseResult insert(@RequestBody @Validated(ValidatorAdd.class) AdChannel channel) {
        return adChannelService.insert(channel);
    }
    @ApiOperation("频道修改")
    @PostMapping("/update")
    public ResponseResult update(@RequestBody @Validated(ValidatorUpdate.class) AdChannel adChannel) {
        return adChannelService.update(adChannel);
    }
    @ApiOperation("频道删除")
    @GetMapping("/del/{id}")
    public ResponseResult del(@PathVariable Integer id) {
        return adChannelService.deleteById(id);
    }
}
